package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.DisciplineEstablishmentTemplate;
import com.wuseguang.springcloud.service.DisciplineEstablishmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DisciplineEstablishmentServiceImpl implements DisciplineEstablishmentService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DisciplineEstablishmentTemplate template;

    /**
     * @描述 查询所有学科
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @Override
    public Map<String, String> querySubjects(String school_id) {
        String sql = "SELECT  id,name  FROM course WHERE school_id=" + school_id;
        return template.getMap(sql, "id", "name");
    }

    /**
     * @描述 删除科目
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @Override
    public int delSubjects(String id) {
        String sql = "DELETE FROM course WHERE id=?";
        return template.updateByParameter(sql, id);
    }


    /**
     * @描述 添加科目
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @Override
    public Long addSubjects(String school_id, String name) {
        String sql = "INSERT INTO course(school_id,NAME)  VALUES(?,?)";
        return template.updateByParameterGetId(sql, school_id, name);
    }


    /**
     * @描述 查询数量
     * @创建人 zm
     * @创建时间 2019/12/15
     */
    @Override
    public int querySubjectsCountandName(String school_id, String name) {
        String sql = "SELECT COUNT(*) count  FROM  course WHERE school_id=? AND NAME=?";
        return template.queryCount(sql, school_id, name);
    }

    /**
     * @描述 修改科目名称
     * @创建人 zm
     * @创建时间 2019/12/11
     */
    @Override
    public int updateSubjects(String id, String name) {
        String sql = "UPDATE course SET NAME=?  WHERE id=?";
        return template.updateByParameter(sql, name, id);
    }

    /**
     * @描述 验证科目是否被使用
     * @创建人 zm
     * @创建时间 2020/2/10
     */
    @Override
    public int querySubjectsById(String id) {
        String sql = " SELECT COUNT(*) count FROM  teacher_role e, roles s ,teacher t WHERE s.course_id=? AND e.role_id=s.id AND e.teacher_id=t.id";
        return template.queryCount(sql, id);
    }
}
